package com.ls.demo.tcp.service.impl;

import com.ls.demo.bean.Command;
import com.ls.demo.bean.RoomBoxInfo;
import com.ls.demo.bean.RoomEnvInfo;
import com.ls.demo.bean.RoomInfo;
import com.ls.demo.http.client.client.HttpClient;
import com.ls.demo.http.client.config.HttpClientProperties;
import com.ls.demo.http.client.view.RoomInfoView;
import com.ls.demo.tcp.namespace.Namespaces;
import com.ls.demo.tcp.service.CommandService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liShan
 * @date 2019/9/3 23:50
 */
@Log
@Service("command_6B")
public class CommandRoomEnvImpl implements CommandService {

    @Resource
    private HttpClient httpClient;

    @Resource
    private HttpClientProperties properties;

    private Namespaces namespaces = Namespaces.getInstance();

    @Override
    public Command execute(ChannelHandlerContext ctx, Command command) {
        RoomBoxInfo info = RoomBoxInfo.getRoomBoxInfo(ctx, command, namespaces);
        List<String> data = command.getDataList();
        RoomEnvInfo envInfo = RoomEnvInfo.builder()
                .envType((short) Integer.parseInt(data.get(0), 16))
                .envValue(Integer.parseInt(data.get(1), 16))
                .build();
        RoomInfo roomInfo = RoomInfo.builder()
                .envInfos(Stream.of(envInfo).collect(Collectors.toList()))
                .roomBoxInfos(Stream.of(info).collect(Collectors.toList()))
                .build();
        RoomInfoView view = httpClient.postForObject(properties.getHost(), properties.getPort(),
                properties.getSendEnvUri(), roomInfo, RoomInfoView.class, null);
        log.info(String.format("StatusCode: %s message: %s", view.getStatusCode(), view.getMessage()));
        return null;
    }
}
